<!-- 商品属性分类 -->
<template>
  <div class="goodsAttrType sys-content">
    <page-mod>
      <el-card>
        <div class="query-box">
          <el-form :model="query" inline>
            <el-form-item label="属性分类名称:">
              <el-input v-model="query.name" placeholder="请输入属性分类名称" clearable/>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button icon="Search" color="#16b777" @click="load(1)">查询</el-button>
            <el-button icon="Plus" color="#FFB800" @click="show = true">新增</el-button>
            <el-button icon="Refresh" color="#FF5722" @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-card>
    </page-mod>
    <page-mod title="商品属性分类数据" icon="Tickets" :showBtn="false" :padBottom="false">
      <el-card v-loading="loading" element-loading-text="拼命加载中">
        <el-table ref="table" :data="dataList" max-height="493" row-key="id" border>
          <el-table-column label="编号" type="index" align="center" width="100"></el-table-column>
          <el-table-column label="属性分类名称" prop="name" align="center"></el-table-column>
          <el-table-column label="操作" align="center" width="400">
            <template #default="{row}">
              <el-button type="primary" icon="SetUp" link @click="toViewAttrOrParams(row, 0)">属性列表</el-button>
              <el-divider direction="vertical" />
              <el-button type="primary" icon="Operation" link @click="toViewAttrOrParams(row, 1)">参数列表</el-button>
              <el-divider direction="vertical" />
              <el-button type="primary" icon="Edit" link @click="toUpdate(row)">修改</el-button>
              <el-divider direction="vertical" />
              <el-button type="danger" icon="Delete" link @click="doDelete(row.attributeCategoryId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          class="paging"
          v-show="query.total"
          small
          layout="total, sizes, prev, pager, next, jumper" 
          :current-page="query.current" 
          :page-size="query.size"
          :total="query.total"
          @current-change="load"
          @size-change="sizeChange">
        </el-pagination>
      </el-card>
    </page-mod>

    <!-- 新增、编辑弹窗 -->
    <el-dialog v-model="show" :title="form.attributeCategoryId ? '属性分类-修改' : '属性分类-新增'" center width="20%" draggable @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" size="default">
        <el-form-item label="名称:" prop="name" required>
          <el-input v-model="form.name" placeholder="请输入属性分类名称" clearable/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="doSubmit">保存</el-button>
        <el-button size="default" @click="show = false">关闭</el-button>
      </template>
    </el-dialog>

    <!-- 属性列表弹窗 -->
    <el-drawer v-model="attrParamsShow" :with-header="false" size="89.6%">
      <attr-params-list :attributeCategoryId="attributeCategoryId" :merchantId="merchantId" :type="type" @close="doChose"></attr-params-list>
    </el-drawer>
  </div>
</template>

<script setup name="goodsAttrType">
  import attrParamsList from './attrParamsList.vue'
  import { cudGoodsAttrTypeApi, getGoodsAttrTypeListPagesApi } from '@/api/api'
  import { ref, reactive, toRefs, getCurrentInstance, onActivated } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties
  
  const formRef = ref(null)

  const currentData = reactive({
    loading: false,
    show: false,
    attrParamsShow: false,
    query: {
      name: '',
      current: 1,
      size: 20,
      total: 0
    },
    dataList: [],
    attributeCategoryId: '',
    merchantId: '',
    type: 0,
    form: {
      attributeCategoryId: '',
      name: ''
    },
    rules: {
      name: [{ required: true, message: '请输入属性分类名称', trigger: ['blur', 'change'] }]
    }
  })

  let {loading, show, attrParamsShow, query, dataList, attributeCategoryId, merchantId, type, form, rules} = toRefs(currentData)

  // 查询
  function load(page) {
    currentData.loading = true
    currentData.query.current = page
    getGoodsAttrTypeListPagesApi(currentData.query).then(res => {
      currentData.loading = false
      if(res.code === '200') {
        currentData.dataList = res.data.pages
        currentData.query.total = res.data.total
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 新增或编辑
  function doSubmit() {
    formRef.value.validate((valid) => {
      if (valid) {
        currentThis.$messageBox.confirm(`确认${currentData.form.attributeCategoryId ? '修改' : '新增'}吗？`).then(() => {
          let loading = currentThis.$loading(`正在${currentData.form.attributeCategoryId ? '修改' : '新增'}`)
          cudGoodsAttrTypeApi(`${currentData.form.attributeCategoryId ? 'update' : 'save'}`, currentData.form).then(res => {
            loading.close()
            if(res.code === '200') {
              currentThis.$message.success(res.message)
              currentData.show = false
              load(currentData.form.attributeCategoryId ? currentData.query.current : 1)
            } else {
              currentThis.$message.error(res.message)
            }
          })
        }).catch(() => {})
      } else {
        return false;
      }
    })
  }

  // 修改前弹窗
  function toUpdate(e) {
    currentData.form.attributeCategoryId = e.attributeCategoryId
    currentData.form.name = e.name
    currentData.show = true
  }

  // 删除
  function doDelete(attributeCategoryId) {
    currentThis.$messageBox.confirm('确认删除吗？').then(() => {
      let loading = currentThis.$loading('正在删除，请耐心等待')
      cudGoodsAttrTypeApi('delete', { id: attributeCategoryId }).then(res => {
        loading.close()
        if(res.code === '200') {
          currentThis.$message.success(res.message)
          load(currentData.query.current)
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }).catch(() => {})
  }

  function toViewAttrOrParams(e, type) {
    currentData.attributeCategoryId = e.attributeCategoryId
    currentData.merchantId = e.merchantId
    currentData.type = type
    currentData.attrParamsShow = true
  }

  // 关闭属性列表，参数列表弹窗
  function doChose() {
    load(currentData.query.current)
    currentData.attrParamsShow = false
  }

  // 重置查询
  function resetSearch() {
    currentData.query.name = ''
    load(1)
  }

  function resetForm() {
    currentData.form = {
      attributeCategoryId: '',
      name: ''
    }
    formRef.value.resetFields()
  }

  function sizeChange(size) {
    currentData.query.size = size
    this.load(1)
  }

  onActivated(() => {
    load(currentData.query.current)
  })
</script>

<style lang="less" scoped>
.goodsAttrType{
  :deep(.el-dialog){
    .el-dialog__body{
      .el-form-item__content{
        >.el-input{
          width: 300px;
        }
        >.el-select{
          .el-input{
            width: 280px !important;
          }
        }
      }
    }
  }
}
</style>